<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="70px">
        <el-form-item label="仓库编码">
          <el-input
            v-model="searchParam.warehouseCode"
            placeholder="请输入仓库编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input
            v-model="searchParam.warehouseName"
            placeholder="请输入仓库名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="外来编号">
          <el-input
            v-model="searchParam.sourceCode"
            placeholder="请输入外来编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!-- <el-form-item label="工厂">
          <el-select
            @keyup.enter="search"
            v-model="searchParam.plantId"
            placeholder="请选择工厂"
            clearable
            filterable
          >
            <el-option
              v-for="item in plantList"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="仓库类型">
          <el-select
            v-model="searchParam.warehouseType"
            placeholder="请选择仓库类型"
            clearable
          >
            <el-option
              v-for="item in dictionaryStore.filterDictList('WarehouseType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="入库冻结">
          <el-select
            v-model="searchParam.lockInbound"
            placeholder="请选择是否入库冻结"
            clearable
          >
            <el-option
              v-for="item in dictionaryStore.filterDictList('YesNo')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="出库冻结">
          <el-select
            v-model="searchParam.lockOutbound"
            placeholder="请选择是否出库冻结"
            clearable
          >
            <el-option
              v-for="item in dictionaryStore.filterDictList('YesNo')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="虚拟仓库">
          <el-select
            v-model="searchParam.isVirtual"
            placeholder="请选择是否虚拟仓库"
            clearable
          >
            <el-option
              v-for="item in dictionaryStore.filterDictList('YesNo')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="searchParam.createTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getcreateTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <!-- v-if="authAllPerm(['npm:wmswarehouse:add'])" -->
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
        <!-- v-if="authAllPerm(['npm:wmswarehouse:delete'])" -->
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="仓库编码"
          prop="warehouseCode"
          min-width="90"
          show-overflow-tooltip
        />
        <el-table-column
          label="仓库名称"
          prop="warehouseName"
          min-width="90"
          show-overflow-tooltip
        />
        <el-table-column
          label="外来编号"
          prop="sourceCode"
          min-width="180"
          show-overflow-tooltip
        />
        <!-- <el-table-column
          label="工厂"
          prop="plantCode"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <!-- <el-table-column
          label="仓库类型"
          prop="warehouseType"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag>
              {{
                dictionaryStore.filterDictName(
                  "WarehouseType",
                  scope.row.warehouseType
                )
              }}
            </el-tag>
          </template>
        </el-table-column> -->
        <el-table-column
          label="入库冻结"
          prop="lockInbound"
          min-width="80"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag>
              {{
                dictionaryStore.filterDictName("YesNo", scope.row.lockInbound)
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="出库冻结"
          prop="lockOutbound"
          min-width="80"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag>
              {{
                dictionaryStore.filterDictName("YesNo", scope.row.lockOutbound)
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="250"
          show-overflow-tooltip
        />
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="150"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="80"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="150"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="更新人"
          prop="lastUpdatedUserName"
          min-width="80"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="210" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <!-- v-if="authAllPerm(['npm:wmswarehouse:check'])" -->
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <!-- v-if="authAllPerm(['npm:wmswarehouse:edit'])" -->
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
            <!-- v-if="authAllPerm(['npm:wmswarehouse:delete'])" -->
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <wmswarehousesimplify-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <wmswarehousesimplify-show
      :visible="addDialogShow.visible"
      :title="addDialogShow.title"
      :data="addDialogShow.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialogShow.readvisible"
      @change-visible="changeVisibleShow"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { authAllPerm } from "@/utils/index";

const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "WMSWMSWAREHOUSESIMPLIFY",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/wms/wmswarehouse/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  warehouseCode: "",
  warehouseName: "",
  plantId: "",
  lockInbound: "",
  lockOutbound: "",
  warehouseType: "",
  isVirtual: "",
  createTimeSearch: [],
  createTimeStart: "",
  createTimeEnd: "",
};

const deleteParam = {
  api: "/wms/wmswarehouse/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlantList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
const addDialogShow = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
// 查询详情
async function getInfo(id: string) {
  request({
    url: `/wms/wmswarehouse/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
async function getInfoShow(id: string) {
  request({
    url: `/wms/wmswarehouse/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialogShow.data = data.data;
      addDialogShow.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
/** 打开仓库表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改仓库";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增仓库";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开仓库表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialogShow.title = "仓库详情";
  getInfoShow(roleObj.id);
  addDialogShow.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
function changeVisibleShow(value: boolean) {
  addDialogShow.visible = value;
  if (!value) {
    addDialogShow.data = {};
  }
}
function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0];
    searchParam.value.createTimeEnd = data[1];
  } else {
    searchParam.value.createTimeStart = "";
    searchParam.value.createTimeEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/wms/wmswarehouse/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
const plantList: any = ref([]);
async function getPlantList() {
  const plants = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = plants.data;
  // searchParam.value.plantId = plantList.value[0].id;
}
</script>
